import tushare as ts
import requests
from helpers import getJsonForData


# year = 2017
# quarter = 3

# data = ts.get_report_data(1992,4)
# print(data)
# exit(0)
for year in range(2010,2018):
    quarter = 0
    print(str(year)+'年')
    while quarter<4:
        quarter += 1
        print(str(quarter)+'季度')
        try:
            # 基金持股
            data = ts.fund_holdings(int(year), int(quarter))
            data = getJsonForData(data)

            request_data = {
                'year': year,
                'quarter': quarter,
                'type': "fund_holdings",
                'data': data,
            }
            print("fund_holdings")
            print(data)
            res = requests.post('http://gst.anyuzhe.com/api/external_data', request_data)

        except Exception as e:
            print('fund_holdings出错:' + str(year) + '--' + str(quarter))

        try:
            # 业绩预告
            data = ts.forecast_data(int(year), int(quarter))
            data = getJsonForData(data)

            request_data = {
                'year': year,
                'quarter': quarter,
                'type': "forecast_data",
                'data': data,
            }
            print("forecast_data")
            print(data)
            res = requests.post('http://gst.anyuzhe.com/api/external_data', request_data)

        except Exception as e:
            print('forecast出错:' + str(year) + '--' + str(quarter))

        try:
            # 业绩报告
            data = ts.get_report_data(int(year), int(quarter))
            data = getJsonForData(data)

            request_data = {
                'year': year,
                'quarter': quarter,
                'type': "get_report_data",
                'data': data,
            }
            print("get_report_data")
            print(data)
            res = requests.post('http://gst.anyuzhe.com/api/external_data', request_data)

        except Exception as e:
            print('report出错:' + str(year) + '--' + str(quarter))

        try:
            # 盈利能力
            data = ts.get_profit_data(int(year), int(quarter))
            data = getJsonForData(data)

            request_data = {
                'year': year,
                'quarter': quarter,
                'type': "get_profit_data",
                'data': data,
            }
            print("get_profit_data")
            print(data)
            requests.post('http://gst.anyuzhe.com/api/external_data', request_data)

        except Exception as e:
            print('profit出错:' + str(year) + '--' + str(quarter))

        try:

            # 营运能力
            data = ts.get_operation_data(int(year), int(quarter))
            data = getJsonForData(data)

            request_data = {
                'year': year,
                'quarter': quarter,
                'type': "get_operation_data",
                'data': data,
            }
            print("get_operation_data")
            print(data)
            requests.post('http://gst.anyuzhe.com/api/external_data', request_data)

        except Exception as e:
            print('operation出错:' + str(year) + '--' + str(quarter))

        try:

            # 成长能力
            data = ts.get_growth_data(int(year), int(quarter))
            data = getJsonForData(data)

            request_data = {
                'year': year,
                'quarter': quarter,
                'type': "get_growth_data",
                'data': data,
            }
            print("get_growth_data")
            print(data)
            requests.post('http://gst.anyuzhe.com/api/external_data', request_data)

        except Exception as e:
            print('growth出错:' + str(year) + '--' + str(quarter))

        try:
            # 偿债能力
            data = ts.get_debtpaying_data(int(year), int(quarter))
            data = getJsonForData(data)

            request_data = {
                'year': year,
                'quarter': quarter,
                'type': "get_debtpaying_data",
                'data': data,
            }
            print("get_debtpaying_data")
            print(data)

            requests.post('http://gst.anyuzhe.com/api/external_data', request_data)

        except Exception as e:
            print('debtpaying出错:' + str(year) + '--' + str(quarter))

        try:
            # 现金流量
            data = ts.get_cashflow_data(int(year), int(quarter))
            data = getJsonForData(data)

            request_data = {
                'year': year,
                'quarter': quarter,
                'type': "get_cashflow_data",
                'data': data,
            }
            print("get_cashflow_data")
            print(data)

            requests.post('http://gst.anyuzhe.com/api/external_data', request_data)
        except Exception as e:
            print('cashflow出错:'+str(year)+'--'+str(quarter))
